package com.esisa.dao.hibernate;

import java.util.List;

import org.hibernate.Query;

import com.esisa.dao.BetailDao;
import com.esisa.models.Betail;


public class BetailDaoHibernate extends AbstractHibernateDao implements BetailDao {

	@Override
	public boolean insert(Betail betail) {
		beginTransaction();
		session.save(betail);
		commit();		
		return true;
	}

	@Override
	public Betail select(Long id) {
		Betail b = null ;
		beginTransaction() ;
		b = (Betail) session.get(Betail.class, id);
		commit();
		return b ;
	}

	@Override
	public void delete(Betail betail) {
		
		beginTransaction() ;
		session.delete(betail);
		commit();
		
	}

	@Override
	public void update(Betail betail) {
		beginTransaction() ;
		session.update(betail);
		commit();
	}

	@Override
	public List<Betail> selectAllBetails() {
		
		List<Betail> list = null;  
		String req = " FROM Betail b" ;
		beginTransaction();
		list = session.createQuery(req).list();
		for (int i = 0; i < list.size(); i++) {
			System.out.println(list.get(i).getId());
		}
		commit();
		return list;
		
	}

	@Override
	public List<Betail> selectAllBetails(int from, int count) {
		List<Betail> list = null;
		beginTransaction();
		String req ="FROM Betail betail" ;
		Query query = session.createQuery(req);
		query.setFirstResult(from);
		query.setMaxResults(count);
		list=query.list() ;
		commit();
		return list;
	}

	@Override
	public int getCountBetails() {
		beginTransaction() ;
		String req = "SELECT COUNT(b) FROM Betail b" ;
		Query query = session.createQuery(req);
		Long count =(Long) query.uniqueResult();		
		commit() ;
		return count.intValue();
	}
	

}
